package spacewar2.gui;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Window;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import spacewar2.configs.SpacewarConfig;
import spacewar2.shadows.Shadow;
import spacewar2.simulator.SpacewarSimulator;

/**
 * Main GUI for the spacewar simulator
 * @author amy
 *
 */
public class SpacewarGUI {
	JFrame mainFrame;
	
	JSpacewarComponent mainComponent;
	
	JSpacewarInfoComponent infoComponent;
	
	JPanel infoPanel, mainPanel;
	
	/**
	 * Make a new GUI
	 * @param config
	 */
	public SpacewarGUI(SpacewarConfig config) {
		super();
		
		mainFrame = new JFrame("Spacewar 2");
		
		infoPanel = new JPanel();
		mainPanel = new JPanel();
		
		infoComponent = new JSpacewarInfoComponent(config.getWidth());
		mainComponent = new JSpacewarComponent(config.getHeight(), config.getWidth());

		infoPanel.add(infoComponent);
		infoPanel.setBorder(BorderFactory.createRaisedBevelBorder());
		infoPanel.setLayout(new BoxLayout(infoPanel, BoxLayout.LINE_AXIS));

		// add the labels (we will change the text later)
//        JLabel label = new JLabel("Timestep: ");
//        label.setBackground(Color.GREEN);
//        
//        infoPanel.add(label);
        

		//infoPanel.add(Box.createRigidArea(new Dimension(0,5)));
		mainPanel.add(mainComponent);
		MyEmptyBorder myEmptyBorder = new MyEmptyBorder();
		mainPanel.setBorder(myEmptyBorder);
		
		Container contentPanel = mainFrame.getContentPane();
		contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.PAGE_AXIS));
		contentPanel.add(infoPanel);
		contentPanel.add(mainPanel);
		//contentPanel.setPreferredSize(new Dimension(config.getWidth(), config.getHeight() + infoComponent.getHeight()));
		//contentPanel.setMinimumSize(new Dimension(config.getWidth(), config.getHeight() + infoComponent.getHeight()));
		
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainFrame.setResizable(false);
		mainFrame.pack();
		mainFrame.setVisible(true);
		//System.out.println("Info component width x height " + infoPanel.getWidth() + " x " + infoPanel.getHeight());
		//System.out.println("Main component width x height " + mainPanel.getWidth() + " x " + mainPanel.getHeight());
	}

//	/**
//	 * @return the main graphics component for the GUI so ships and the like can be drawn on it
//	 */
//	public Graphics2D getGraphics() {
//		return (Graphics2D) mainComponent.getGraphics();
//	}
//
	/**
	 * Redraws the graphics
	 * @param spacewarSimulator
	 */
	public void redraw(SpacewarSimulator spacewarSimulator) {
		infoComponent.setSimulator(spacewarSimulator);
		//mainFrame.paintComponents(getGraphics());
		mainFrame.repaint();
	}

	/**
	 * Registers the shadow with the component which is what does the main drawing
	 * @param shadow
	 */
	public void addShadow(Shadow shadow) {
		mainComponent.addShadow(shadow);
	}
	
	/**
	 * Remove this shadow from being drawn
	 * @param shadow
	 */
	public void removeShadow(Shadow shadow) {
		mainComponent.removeShadow(shadow);
	}
	
	
	
}
